<style scoped>
  .page-infinite-wrapper {
    background: #e3e3e3;
  }
  .weui-form-preview {
    margin: 5px;
    border-radius: 5px;
  }
  .rj-cell-child.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell-child label {
    color: #333;
    font-size: 14px;
  }
  .rj-cell-child span {
    padding-left: 20px;
    font-size: 14px;
    color: #333;
  }
  .rj-cell-child .tip {
    font-size: 16px;
    color: #333;
  }
  .rj-cell-child i {
    font-size: 18px;
    font-style: normal;
    color: #333;
  }
  .weui-form-preview__ft .weui-form-preview {
    background: #f5f5ef;
  }
  .weui-form-preview__ft {
    line-height: normal;
  }
   .weui_tab_bd {
    background: #eee;
  }
  .weui-form-preview {
    background-color: #fff;
    margin: 5px;
    border-radius: 5px;
  }
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui_navbar {
    white-space: nowrap;
    overflow: scroll;
    display: block !important;
    border-bottom: 1px solid #BCBAB6;
    font-size: 0;
    /*background: #fafafa;*/
  }
  .weui_navbar_item {
    font-size: 14px;
    display: inline-block;
    width: 50%;
    padding: 13px 0;
    background: #fff;
    position: relative;
    color: #888;
  }
  .weui_navbar_item.weui_bar_item_on {
    background-color: #f5f5f5;
    color: #00913a;
  }
  .weui_navbar:after {
    border-bottom: 0;
  }
  .weui_navbar_item:after {
    content: " ";
    position: absolute;
    right: 0;
    top: 0;
    width: 1px;
    height: 100%;
    border-right: 1px solid #cccccc;
    color: #cccccc;
    -webkit-transform-origin: 100% 0;
    transform-origin: 100% 0;
    -webkit-transform: scaleX(0.5);
    transform: scaleX(0.5);
    font-size: 10px;
  }
</style>

<template>
  <div class="comon">
    <SearchBar @doSearch="doSearch" />
    <div class="weui_tab">
      <div class="weui_navbar order-tabhd">
        <a class="weui_navbar_item " :class="{ weui_bar_item_on: batchstatus == 1 }" href="javaScript:;" @click="switchStatus(1)">当前</a>
        <a class="weui_navbar_item " :class="{ weui_bar_item_on: batchstatus == 2 }" href="javaScript:;" @click="switchStatus(2)">历史</a>
      </div>
      <div class="weui_tab_bd" style="background-color: #fff">
        <div class="page-infinite-wrapper" ref="wrapper" :style="{ height: wrapperHeight + 'px' }" >
          <mescroll-vue ref="mescroll" id="wrapper" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit">
            <div class="weui-form-preview" v-for="(item, index) in data" :key="item.guid" :id="item.guid" :ref="'mark' + index">
              <vue-water-mark-tools :fatherId="item.guid" :rowNum="1" :colNum="1" :txt="'水印文本'"></vue-water-mark-tools>
              <!--              <div class="weui-form-preview__hd" @click="viewItem(item.guid)">-->
              <div class="weui-form-preview__hd">
                <label class="weui-form-preview__label">订单号</label>
                <em class="weui-form-preview__value">{{item.code}}</em>
              </div>
              <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">种植户</label>
                  <span class="weui-form-preview__value">{{item.farmer}}(<i>{{item.typename}}</i>)</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">批次</label>
                  <span class="weui-form-preview__value">{{item.batchname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">订单年份</label>
                  <span class="weui-form-preview__value">{{item.yearoforder}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">部门</label>
                  <span class="weui-form-preview__value">{{item.department}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">种植面积</label>
                  <span class="weui-form-preview__value">{{item.orderarea}} 亩</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">品种</label>
                  <span class="weui-form-preview__value">{{item.productname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">订单负责人</label>
                  <span class="weui-form-preview__value">{{item.orderownername}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">种植负责人</label>
                  <span class="weui-form-preview__value">{{item.plantownername}}</span>
                </div>
              <div class="weui-form-preview__ft" style="line-height: 50px;">
                  <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:" @click="checkRecord(item.guid)" >田检记录</a>
                  <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:" @click="handleRecord(item.guid)" >农事记录</a>
              </div>
            </div>
            </div>
          </mescroll-vue>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/babel">
  import MescrollVue from 'mescroll.js/mescroll.vue'
  import SearchBar from '../../common/SearchBar'
  export default {
    name: 'OrderList',
    components: {SearchBar, MescrollVue},
    data() {
      return {
        permissions:[],
        permission: {},
        postParams: {},
        batchstatus: 1,
        mescroll: null, // mescroll实例对象
        mescrollDown: {},
        mescrollUp: { // 上拉加载的配置.
          callback: this.upCallback, // 上拉回调,此处简写; 相当于 callback: function(page, mescroll) { }
          page: {
            num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
            size: 5 //每页数据条数,默认10
          },
          htmlNodata: '<p class="upwarp-nodata">-- 我是有底线的 --</p>',
          noMoreSize: 1,
          empty: {
            warpId: "wrapper", //父布局的id (1.3.5版本支持传入dom元素)
            tip: "暂无相关数据~" //提示
          }
        },
        wrapperHeight: 600,
        data: [],
      }
    },
    created() {
      //列表页头部不允许滑动
      document.getElementsByTagName("body")[0].className="bg";
    },
    mounted() {
      //根据内容高度适配wraper高度
      this.wrapperHeight = document.documentElement.clientHeight - this.$refs.wrapper.getBoundingClientRect().top - 40;
    },
    methods: {
      checkRecord: function(id) {
        this.$router.push({name: 'CheckRecord', query: {id: id}});
      },
      handleRecord: function(id) {
        this.$router.push({name: 'HandleRecord', query: {id: id}});
      },
      switchStatus: function (sta) {
        this.batchstatus = sta;
        this.getPageData();
      },
      mescrollInit (mescroll) {
        this.mescroll = mescroll  // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
      },
      upCallback (page, mescroll) {
        this.postParams.pageNo = page.num;
        this.postParams.pageSize = page.size;
        this.postParams.functiontype = 24;
        this.postParams['entity.batchstatus'] = this.batchstatus;
        // 联网请求
        this.axios({
          method: 'get',
          url: '/farmer/Farmerorder!list.action',
          params: this.postParams
        }).then((response) => {
          if (response.data.msg == 'success') {
            let res = response.data != undefined ? response.data : {pagelist: {rows: []}};


            //制作水印
            // response.data.pagelist.rows.forEach((item, index) => {
              // this.$refs['mark' + (this.data.length + index)][0].initPage();
            // })

            // 如果是第一页需手动置空列表
            if (page.num === 1 && this.data.length != 0) this.data = [];
            // 把请求到的数据添加到列表
            this.data = this.data.concat(res.pagelist.rows);
            // 数据渲染成功后,隐藏下拉刷新的状态
            this.$nextTick(() => {
              mescroll.endSuccess(res.pagelist.rows.length)
            })
            //将获取上一访问列表临时页码恢复正常
            if (this.mescrollUp.page.size > 5) {
              this.mescrollUp.page.num = this.$store.state.list.pageNo;
              this.mescrollUp.page.size = 5;
            }
          } else if (response.data
            .status == 'false') {
            this.toastText(response.data.result);
          } else {
            this.toastError("系统错误");
          }

        }).catch((e) => {
          // 联网失败的回调,隐藏下拉刷新和上拉加载的状态;
          mescroll.endErr()
        })
      },
      getPageData: function () {
        $('#wrapper').scrollTop(0);
        this.data = [];
        this.mescroll.resetUpScroll();
      },
      doSearch: function(keywords) {
        this.postParams.search = keywords;
        this.getPageData();
      },

    },
    activated () {
      //列表页头部不允许滑动
      document.getElementsByTagName("body")[0].className="bg";

      //判断是否是返回上一页
      if (this.$store.state.list.reffer != this.$options.name) return;
      //获取上一次访问列表数据
      this.mescrollUp.page.num = 0;
      this.mescrollUp.page.size = this.$store.state.list.pageNo * this.mescrollUp.page.size;
      this.getPageData();
      //定位
      this.$nextTick(function(){
        let position = this.$store.state.list.postion //返回页面取出来
        $('#wrapper').scrollTop(position);
        let status = this.$store.state.list.status;
        this.postParams.status = status;
      })
    },
    beforeRouteLeave(to, from, next){
      //取消列表页头部不允许滑动
      document.body.removeAttribute("class","bg");

      let position = $('#wrapper').scrollTop();
      let status = this.postParams.status;
      this.$store.commit('setPosition', position) //离开路由时把位置存起来
      this.$store.commit('setStatus', status)
      this.$store.commit('setPageNo', this.mescrollUp.page.num);
      this.$store.commit('setReffer', this.$options.name);
      next()
    }
  };
</script>
